package com.lastproject.ejb.util;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.lastproject.dto.InterviewDTO;
import com.lastproject.ejb.entities.Candidate;
import com.lastproject.ejb.entities.Employee;
import com.lastproject.ejb.entities.Interview;

@Stateless
@LocalBean
public class InterviewConversion {

	@PersistenceContext
	EntityManager em;

	public InterviewDTO fromEntity(Interview interview) {
		InterviewDTO interviewDTO = new InterviewDTO();

		interviewDTO.setId(interview.getId());

		interviewDTO.setDate(interview.getDate());

		interviewDTO.setDetails(interview.getDetails());
		
		interviewDTO.setIdCandidate(interview.getCandidate().getId());
		
		interviewDTO.setIdEmployee(interview.getInterviewer().getId());

		return interviewDTO;
	}

	public Interview fromDTO(InterviewDTO interviewDTO){
		
		Interview interview = em.find(Interview.class, interviewDTO.getId());

        if (interview == null) {

                interview = new Interview();

        }

        interview.setId(interviewDTO.getId());
        
        interview.setDate(interviewDTO.getDate());
        
        interview.setDetails(interviewDTO.getDetails());
        
        /*interview.setCandidate(em.find(Candidate.class, interviewDTO.getIdCandidate()));
        
        interview.setInterviewer(em.find(Employee.class, interviewDTO.getIdEmployee()));*/
        
        Candidate candi = em.find(Candidate.class, interviewDTO.getIdCandidate());
        
        interview.setCandidate(candi);
        
        Employee emp = em.find(Employee.class, interviewDTO.getIdEmployee());
        
        interview.setInterviewer(emp);
        

        return interview;


		
	}
}
